#include <QCoreApplication>
#include <QString>
#include <QDebug>

QString encrypt(QString plainText,QString key);
QString decrypt(QString cipherText,QString key);
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString plainText = "ThisIsAnExample";
    QString key = "20210729";
    QString encryptString;
    QString decryptString;
    qDebug() << "Plain Text:" <<plainText << endl;
    encryptString= encrypt(plainText, key);
    qDebug() << "Encrypted Text:" <<encryptString << endl;
    decryptString = decrypt(encryptString, key);
    qDebug() << "Decrypt Text:" <<decryptString << endl;
    return a.exec();
}

QString encrypt(QString plainText,QString key)
{
    QString encryptText;
    QChar temp;
    int strLength = plainText.size();
    int keyLength = key.size();
    for(int i = 0; i < strLength; i++)
    {
        temp = plainText[i].toLatin1()-(key[i%keyLength].toLatin1()-'0');
        encryptText.append(temp);
    }
    return encryptText;
}

QString decrypt(QString cipherText,QString key)
{
    QString decryptText;
    QChar temp;
    int strLength = cipherText.size();
    int keyLength = key.size();
    for(int i = 0; i < strLength; i++)
    {
        temp = cipherText[i].toLatin1()+(key[i%keyLength].toLatin1()-'0');
        decryptText.append(temp);
    }
    return decryptText;
}
